/**
 * @(#)MiguUserChcekServiceImpl.java 2017年12月6日
 * Copyright(C) 2017 ZHEXIN IT CO.,LTD. All rights reserved.
 */
package org.jsbd.boss.service.impl;

import java.util.HashSet;
import java.util.Iterator;
import java.util.List;
import java.util.Set;
import java.util.concurrent.ArrayBlockingQueue;
import java.util.concurrent.TimeUnit;

import javax.annotation.Resource;

import org.jsbd.boss.common.redis.RedisClient;
import org.jsbd.boss.dao.IMiguUserDAO;
import org.jsbd.boss.domian.channel.ApkLoginTypeConfig;
import org.jsbd.boss.domian.channel.MiguUser;
import org.jsbd.boss.enums.ApkBaseEnum;
import org.jsbd.boss.enums.LoginTypeEnum;
import org.jsbd.boss.service.IApkLoginTypeService;
import org.jsbd.boss.service.IMiguUserCheckService;
import org.jsbd.boss.task.MiguUserCheckThread;
import org.jsbd.boss.util.CacheKey;
import org.jsbd.boss.util.threads.ThreadPoolExecutor;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

/**
 * 咪咕校验
 * 
 * @author <a href="mailto:xupengcheng@zhexinit.com" >徐鹏程</a>
 * @version 1.0.0
 */
@Service
public class MiguUserChcekServiceImpl implements IMiguUserCheckService {
	public static final Logger LOGGER = LoggerFactory.getLogger(MiguUserChcekServiceImpl.class);


	@Resource(name = "allMiguUserDAO")
	private IMiguUserDAO allMiguUserDAO;

	@Resource
	private RedisClient redisClient;

	private ThreadPoolExecutor poolExecutor = new ThreadPoolExecutor(10, 10, 20, TimeUnit.SECONDS, new ArrayBlockingQueue<Runnable>(100));

	@Resource
	private IApkLoginTypeService apkLoginTypeService;

	/**
	 * 
	 * getBaseOrApkIdSet:咪咕配置有可能中途变化过，所以这个需要查询三种类型的
	 * 
	 * @author <a href="mailto:xupengcheng@zhexinit.com" >徐鹏程</a>
	 * @return
	 */
	private Set<Integer> getBaseOrApkIdSet() {
		Set<Integer> baseOrApkIdSet = new HashSet<Integer>();
		List<ApkLoginTypeConfig> loginList = apkLoginTypeService.selectApkLoginConfig(LoginTypeEnum.MIGU.getType());
		List<ApkLoginTypeConfig> selfLoginList = apkLoginTypeService.selectApkLoginConfig(LoginTypeEnum.SELF_MOBILE_MIGU.getType());
		List<ApkLoginTypeConfig> thirdLoginList = apkLoginTypeService.selectApkLoginConfig(LoginTypeEnum.THIRD_MIGU.getType());
		loginList.addAll(selfLoginList);
		loginList.addAll(thirdLoginList);
		for (ApkLoginTypeConfig apkConfig : loginList) {
			Integer base = apkConfig.getBase();
			Integer apkId = apkConfig.getApkId();
			if (base != null && base.intValue() == ApkBaseEnum.OTHER.getType()) {
				baseOrApkIdSet.add(apkId);
			} else {
				baseOrApkIdSet.add(base);
			}
		}
		return baseOrApkIdSet;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see org.jsbd.boss.service.IMiguUserCheckService#miguUserCheck()
	 */
	@Override
	public void checkMiguUser() {
//		checkExcepitonUser();
//		checkUser(LoginTypeEnum.MIGU.getType());
//		checkUser(LoginTypeEnum.THIRD_MIGU.getType());
//		checkUser(LoginTypeEnum.SELF_MOBILE_MIGU.getType());
	}

	/**
	 * 
	 * checkExcepitonUser:校验异常的账号
	 * @author <a href="mailto:xupengcheng@zhexinit.com" >徐鹏程</a>
	 */
	private void checkExcepitonUser() {
		Set<Integer> baseOrApkIdSet = getBaseOrApkIdSet();
		while (true) {
			MiguUser miguUser = (MiguUser) redisClient.pollFromQueue(CacheKey.MIGU_CHECK_USER_LIST);
			if (miguUser != null) {
				poolExecutor.execute(new MiguUserCheckThread(miguUser, baseOrApkIdSet));
			} else {
				break;
			}
		}

	}

//	private void checkUser(int userType) {
//		try {
//			String indexKey = CacheKey.MIGU_IMPORT_INDEX;
//			Integer start = 0;
//			Integer length = 100;
//			String fieldKey = "userindex" + userType;
//			Object loginIndex = redisClient.getHash(indexKey, fieldKey);
//			if (loginIndex != null) {
//				start = Integer.parseInt(loginIndex.toString());
//			}
//			List<MiguUser> userList = null;
//			if (userType == LoginTypeEnum.MIGU.getType()) {
//				userList = miguUserDAO.selectUserList(start, null, length, null);
//			} else if (userType == LoginTypeEnum.THIRD_MIGU.getType()) {
//				userList = thirdMiguUserDAO.selectUserList(start, null, length, null);
//			} else if (userType == LoginTypeEnum.SELF_MOBILE_MIGU.getType()) {
//				userList = selfMiguUserDAO.selectUserList(start, null, length, null);
//			}
//			if (!CollectionUtils.isEmpty(userList)) {
//				Set<Integer> baseOrApkIdSet = getBaseOrApkIdSet();
//				Iterator<MiguUser> iterator = userList.iterator();
//				while (iterator.hasNext()) {
//					MiguUser miguUser = iterator.next();
//					if (start < miguUser.getId()) {
//						start = miguUser.getId();
//					}
//					miguUser.setDataSource(userType);
//					redisClient.putHash(indexKey, fieldKey, start);
//					poolExecutor.execute(new MiguUserCheckThread(miguUser, baseOrApkIdSet));
//				}
//			}
//			LOGGER.warn("咪咕用户校验完成 {} 获取到的数据大小 {} 下标 {} ", new Object[] { userType, userList.size(), start });
//		} catch (Exception e) {
//			LOGGER.error("咪咕用户校验", e);
//		}
//
//	}

}
